package cn.iocoder.yudao.module.system.controller.admin.examcontrolline;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.system.controller.admin.examcontrolline.vo.*;
import cn.iocoder.yudao.module.system.convert.examcontrolline.ExamControlLineConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.examcontrolline.ExamControlLineDO;
import cn.iocoder.yudao.module.system.dal.dataobject.examscoreconversion.ExamScoreConversionDO;
import cn.iocoder.yudao.module.system.service.examcontrolline.ExamControlLineService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 特控线配置")
@RestController
@RequestMapping("/system/exam-control-line")
@Validated
public class ExamControlLineController {

    @Resource
    private ExamControlLineService examControlLineService;

    @PostMapping("/create")
    @Operation(summary = "创建特控线配置")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:create')")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Long> createExamControlLine(@Valid @RequestBody ExamControlLineCreateReqVO createReqVO) {
        return success(examControlLineService.createExamControlLine(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新特控线配置")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:update')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> updateExamControlLine(@Valid @RequestBody ExamControlLineUpdateReqVO updateReqVO) {
        examControlLineService.updateExamControlLine(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除特控线配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:delete')")
    @ApiAccessLog(operateType = DELETE)
    public CommonResult<Boolean> deleteExamControlLine(@RequestParam("id") Long id) {
        examControlLineService.deleteExamControlLine(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得特控线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:query')")
    @DataPermission
    public CommonResult<ExamControlLineRespVO> getExamControlLine(@RequestParam("id") Long id) {
        ExamControlLineDO examControlLine = examControlLineService.getExamControlLine(id);
        return success(ExamControlLineConvert.INSTANCE.convert(examControlLine));
    }

    @GetMapping("/list")
    @Operation(summary = "获得特控线配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:query')")
    public CommonResult<List<ExamControlLineRespVO>> getExamControlLineList(@RequestParam("ids") Collection<Long> ids) {
        List<ExamControlLineDO> list = examControlLineService.getExamControlLineList(ids);
        return success(ExamControlLineConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得特控线分页")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:query')")
    @DataPermission
    public CommonResult<PageResult<ExamControlLineRespVO>> getExamControlLinePage(@Valid ExamControlLinePageReqVO pageReqVO) {
        PageResult<ExamControlLineDO> pageResult = examControlLineService.getExamControlLinePage(pageReqVO);
        return success(ExamControlLineConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出特控线 Excel")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:export')")
    @DataPermission
    @ApiAccessLog(operateType = EXPORT)
    public void exportExamControlLineExcel(@Valid ExamControlLinePageReqVO exportReqVO,
                                          HttpServletResponse response) throws IOException {
        List<ExamControlLineDO> list = examControlLineService.getExamControlLineList(exportReqVO);
        // 导出 Excel
        List<ExamControlLineRespVO> datas = ExamControlLineConvert.INSTANCE.convertList(list);
        ExcelUtils.write(response, "特控线配置.xls", "数据", ExamControlLineRespVO.class, datas);
    }

    @GetMapping("/list-by-exam")
    @Operation(summary = "根据考试ID获取特控线列表")
    @Parameter(name = "examId", description = "考试ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:query')")
    public CommonResult<List<ExamControlLineRespVO>> getExamControlLineListByExam(@RequestParam("examId") Long examId) {
        List<ExamControlLineDO> list = examControlLineService.getExamControlLineListByExamId(examId);
        return success(ExamControlLineConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/convert-score")
    @Operation(summary = "成绩换算")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:convert')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<ExamScoreConversionRespVO> convertScore(
            @RequestParam("examId") Long examId,
            @RequestParam("studentId") Long studentId,
            @RequestParam("originalScore") BigDecimal originalScore,
            @RequestParam("controlLineId") Long controlLineId) {
        return success(examControlLineService.convertScore(examId, studentId, originalScore, controlLineId));
    }

    @PostMapping("/batch-convert-score")
    @Operation(summary = "批量成绩换算")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:convert')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<List<ExamScoreConversionRespVO>> batchConvertScore(
            @RequestParam("examId") Long examId,
            @RequestParam("controlLineId") Long controlLineId) {
        return success(examControlLineService.batchConvertScore(examId, controlLineId));
    }

    @GetMapping("/conversion-records")
    @Operation(summary = "获取学生成绩换算记录")
    @Parameter(name = "studentId", description = "学生ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:query')")
    public CommonResult<List<ExamScoreConversionRespVO>> getConversionRecords(@RequestParam("studentId") Long studentId) {
        List<ExamScoreConversionDO> records = examControlLineService.getConversionRecordsByStudentId(studentId);
        return success(ExamControlLineConvert.INSTANCE.convertConversionList(records));
    }

    @GetMapping("/college-recommendations")
    @Operation(summary = "获取院校推荐")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:query')")
    public CommonResult<List<CollegeRecommendationRespVO>> getCollegeRecommendations(
            @RequestParam("convertedScore") BigDecimal convertedScore,
            @RequestParam("estimatedRank") Integer estimatedRank,
            @RequestParam("province") String province) {
        return success(examControlLineService.getCollegeRecommendations(convertedScore, estimatedRank, province));
    }

    @GetMapping("/major-recommendations")
    @Operation(summary = "获取专业推荐")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:query')")
    public CommonResult<List<MajorRecommendationRespVO>> getMajorRecommendations(
            @RequestParam("convertedScore") BigDecimal convertedScore,
            @RequestParam("estimatedRank") Integer estimatedRank,
            @RequestParam("province") String province,
            @RequestParam("subjectCombination") String subjectCombination) {
        return success(examControlLineService.getMajorRecommendations(convertedScore, estimatedRank, province, subjectCombination));
    }

    @GetMapping("/admission-advice")
    @Operation(summary = "生成学生报考建议")
    @PreAuthorize("@ss.hasPermission('system:exam-control-line:query')")
    public CommonResult<StudentAdmissionAdviceRespVO> generateAdmissionAdvice(
            @RequestParam("studentId") Long studentId,
            @RequestParam("examId") Long examId) {
        return success(examControlLineService.generateAdmissionAdvice(studentId, examId));
    }

}
